<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  name: 'BarChart',
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart1'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    item: {
      type: Object
    },
    y_data: {
      type: Array
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    console.log(this.y_data)
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        // title: { text: '身体质量指数' },
        backgroundColor: 'white',
        tooltip: {
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          },
          trigger: 'axis',
          formatter: function(param) {
            // console.log(param)
            param = param[0]
            return [
              '标准' + '<br/>' + '<hr size=1 style="margin: 3px 0">' +
              '<h1 style="font-size:14px;color:white;margin:0 25px 0 0">' + param.data + '</h1>'
            ].join('')
          }
        },
        xAxis: {
          type: 'category',
          // data: ['1月', '2月', '3月', '4月', '5月'],
          axisTick: {
            alignWithLabel: true
          },
          axisLine: {
            lineStyle: {
              color: '#CCCCCC'
            }
          },
          axisLabel: {
            textStyle: {
              color: 'black'// 坐标值得具体的颜色
            }
          }
        },
        yAxis: {
          type: 'value',
          // data: ['20', '40', '60', '80', '100', '120'],
          splitLine: {
            show: true,
            lineStyle: {
              type: 'dashed'
            }
          },
          axisLine: {
            lineStyle: {
              color: 'white'
            }
          },
          axisLabel: {
            textStyle: {
              color: 'black'// 坐标值得具体的颜色

            }
          }
        },
        // color: ['#5dd39a', '#f8c026'],
        grid: {
          x: '5%', // 相当于距离左边效果:padding-left
          y: '15%', // 相当于距离上边效果:padding-top
          right: '3%',
          bottom: '3%',
          containLabel: true
        },
        series: [
          {
            stack: '总量',
            name: '达标',
            data: this.y_data,
            type: 'bar',
            label: {
              show: true,
              position: 'top',
              formatter: '{c}%'// c后面加单位
            },
            itemStyle: {
              normal: {
                color: function(params) {
                  // console.log('params')
                  var colorList = ['rgba(72, 105, 248, 1)', 'rgba(24, 144, 255, 1)', 'rgba(65, 203, 137, 1)', 'rgba(247, 181, 0, 1)', 'rgba(244, 40, 80, 1)']
                  return colorList[params.dataIndex]
                }
              }
            },
            barWidth: 60
          }

        ]
      })
    }
  }
}
</script>
